package com.lzc.tlmail.tlmailproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lzc.tlmail.tlmailproduct.dao.CategoryBrandRelationDao;
import com.lzc.tlmail.tlmailproduct.entity.CategoryBrandRelationEntity;
import com.lzc.tlmail.tlmailproduct.service.CategoryBrandRelationService;
import com.lzc.tlmail.tlmailproduct.vo.BrandVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzc.common.utils.PageUtils;
import com.lzc.common.utils.Query;

import com.lzc.tlmail.tlmailproduct.dao.BrandDao;
import com.lzc.tlmail.tlmailproduct.entity.BrandEntity;
import com.lzc.tlmail.tlmailproduct.service.BrandService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {


    @Resource
    private CategoryBrandRelationService categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<BrandEntity> queryWrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            queryWrapper.eq("brand_id",key).or().like("name",key);
        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void updateDetails(BrandEntity brand) {
        this.updateById(brand);
        if(!StringUtils.isEmpty(brand.getName())){
            categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());

        }
    }
    /**
     * 根据指定的品牌多个id得到品牌
     */
    @Override
    public List<BrandVo> queryBransByIds(List<Long> brandsId) {
        List<BrandEntity> brandEntities = this.baseMapper.selectList(new LambdaQueryWrapper<BrandEntity>()
                .in(BrandEntity::getBrandId, brandsId));
        List<BrandVo> brandVos = brandEntities.stream().map(brand -> {
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandId(brand.getBrandId());
            brandVo.setBrandName(brand.getName());
            return brandVo;
        }).collect(Collectors.toList());
        return brandVos;
    }


}